package controllers

import (
	"log"
	m "myk8s/models"

	beego "github.com/beego/beego/v2/server/web"
	"github.com/tidwall/gjson"
)

type NodeController struct {
	beego.Controller
}

func (this *NodeController) List() {
	clusterId := this.GetString("clusterId")
	nodeList, err := m.NodeList(clusterId)
	msg := "success"
	code := 0
	count := len(nodeList)
	if err != nil {
		log.Println(err)
		msg = err.Error()
		code = -1
	}
	this.Data["json"] = &map[string]interface{}{"code": code, "msg": msg, "count": count, "data": &nodeList}
	this.ServeJSON()
}

//排水
func (this *NodeController) Drain() {
	clusterId := this.GetString("clusterId")
	nodeName := this.GetString("nodeName")
	code := 0
	msg := "success"
	err := m.NodeDrain(clusterId, nodeName)
	if err != nil {
		code = -1
		msg = err.Error()
		log.Printf("[ERROR] NodeDrain Fail:%s\n", err)
	}
	this.Data["json"] = &map[string]interface{}{"code": code, "msg": msg}
	this.ServeJSON()
}

//调度
func (this *NodeController) Unschedulable() {
	clusterId := this.GetString("clusterId")
	nodeName := this.GetString("nodeName")
	code := 0
	msg := "success"
	gp := gjson.ParseBytes(this.Ctx.Input.RequestBody)
	unschedulableValue := gp.Get("unschedulable").Bool()
	err := m.NodeUnschedulable(clusterId, nodeName, unschedulableValue)
	if err != nil {
		code = -1
		msg = err.Error()
		log.Printf("[ERROR] Unschedulable Fail:%s\n", err)
	}
	this.Data["json"] = &map[string]interface{}{"code": code, "msg": msg}
	this.ServeJSON()
}
